package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisPool;

import java.util.List;

@Service(interfaceClass = SetmealService.class)
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private JedisPool jedisPool;

    /**
     * 添加套餐
     *
     * @param checkgroupIds
     * @param setmeal
     */
    @Override
    public void add(Integer[] checkgroupIds, Setmeal setmeal) {
        setmealMapper.add(setmeal);
        if (checkgroupIds != null) {
            setSetmealAndCheckGroup(setmeal.getId(), checkgroupIds);
        }
        //若图片为空，则不存储进redis中
        if (setmeal.getImg() != null) {
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES, setmeal.getImg());
        }


    }

    /**
     * 按条件查询页数
     *
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {

        //使用mybatis的分页插件,查询语句会自动拦截，所以顺序不会变
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        List<Setmeal> setmealList = setmealMapper.findByCondition(queryPageBean.getQueryString());
        PageInfo<Setmeal> pageInfo = new PageInfo<>(setmealList);
        return new PageResult(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 为移动端查询全部套餐
     *
     * @return
     */
    @Override
    public List<Setmeal> findAll() {
        String setmealListJson = jedisPool.getResource().get(RedisConstant.SETMEAL_DATA_LIST_RESOURCES);
        if (null == setmealListJson || "".equals(setmealListJson)) {
            List<Setmeal> setmealList = setmealMapper.findAll();
            setmealListJson = JSON.toJSONString(setmealList);
            jedisPool.getResource().set(RedisConstant.SETMEAL_DATA_LIST_RESOURCES, setmealListJson);
        }
        List<Setmeal> setmealList = JSON.parseObject(setmealListJson, List.class);
        return setmealList;
    }

    /**
     *
     * 根据id获取套餐所有信息(包括检查组和检查项)
     * redis中的key为 id + REDISConstant信息
     *
     * @param SetmealId
     * @return
     */
    @Override
    public Setmeal findById(Integer SetmealId) {
        String setmealIdJson = jedisPool.getResource().get(SetmealId + RedisConstant.SETMEAL_DATA_INDIVIDUAL_RESOURCES);
        if (null == setmealIdJson || "".equals(setmealIdJson)) {
            Setmeal setmealById = setmealMapper.findById(SetmealId);
            setmealIdJson = JSON.toJSONString(setmealById);
            jedisPool.getResource().set(SetmealId + RedisConstant.SETMEAL_DATA_INDIVIDUAL_RESOURCES, setmealIdJson);
        }
        Setmeal setmealByID = JSON.parseObject(setmealIdJson, Setmeal.class);
        return setmealByID;
    }


    private void setSetmealAndCheckGroup(Integer SetmealId, Integer[] checkgroupIds) {
        if (SetmealId != 0 & (checkgroupIds != null & checkgroupIds.length != 0)) {
            setmealMapper.setSetmealAndCheckGroup(SetmealId, checkgroupIds);
        }
    }

}
